/* Copyright (C) 2010 MoSync AB

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2, as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/

#include "UIBuilder.h"
#include "MAHeaders.h"
#include "../std.h"
#include "Colours.h"

void setSKPadding(Widget* w) 
{
	w->setPaddingLeft(HORIZONTALPADDING);
	w->setPaddingBottom(4);
	w->setPaddingRight(HORIZONTALPADDING);
	w->setPaddingTop(3);
}

void setLabelPadding(Widget* w) 
{
	w->setPaddingLeft(HORIZONTALPADDING);
	w->setPaddingBottom(VERTICALPADDING);
	w->setPaddingRight(HORIZONTALPADDING);
	w->setPaddingTop(VERTICALPADDING);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Create a new label widget with the plain skin
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Label* createLabel(const char* str, int height) 
{
	////lprintfln("Creating label: Text is %s, height is %d", str, height);
	Label* label;
	label = new Label(0,0, APPCONTROLLER->getScreenWidth(), height, NULL, str, 0, APPCONTROLLER->getFont("default"));

	//label->setBackgroundColor(WHITE);
	label->setHorizontalAlignment(Label::HA_LEFT);
	label->setMultiLine(true);
	//label->setAutoSizeY(true);
	label->setAutoSizeX(false);
	setLabelPadding(label);
	label->setSkin(APPCONTROLLER->getSkin("default"));
	return label;
}

Label* createCenterLabel(const char* str, int height)
{
	////lprintfln("Creating label: Text is %s, height is %d", str, height);
	Label* label;
	label = new Label(0,0, APPCONTROLLER->getScreenWidth(), height, NULL, str, 0, APPCONTROLLER->getFont("default"));

	//label->setBackgroundColor(WHITE);
	label->setHorizontalAlignment(Label::HA_CENTER);
	label->setVerticalAlignment(Label::VA_CENTER);
	label->setMultiLine(true);
	//label->setAutoSizeY(true);
	label->setAutoSizeX(false);
	//setLabelPadding(label);
	label->setSkin(APPCONTROLLER->getSkin("default"));
	return label;
}

Label* createLabel(String& str, int height)
{
  return createLabel(str.c_str(), height);
}

// Create a new image widget
Image* createImage(int res)
{
	////lprintfln("Creating image");
	Image* i = new Image(0, 0, APPCONTROLLER->getScreenWidth(), APPCONTROLLER->getScreenHeight(), NULL, true, true, res);
	i->setDrawBackground(false);
	return i;
}


// Create a new menu image  widget
Widget* createMenuImage(int res, char* caption, Font* f, WidgetSkin* skin)
{
	MenuButton* mb;// = new MenuButton(0, 0, 74, 94, NULL, res, OVERLAY, HIGHLIGHT, caption);
	//Don't show the label in this version
	//MenuButton* mb = new MenuButton(0, 0, 74, 74, NULL, res, OVERLAY, HIGHLIGHT, "");
	mb->setSkin(skin);
	mb->setFont(f);
	return mb;
}

Widget* createSoftKeyBar(int height, const char *left, const char *right) {
	Layout *layout = new Layout(0, 0, APPCONTROLLER->getScreenWidth(), height, NULL, 2, 1);
	Label *label;

	label = new Label(0,0, APPCONTROLLER->getScreenWidth()/2, height, NULL, left, 0, APPCONTROLLER->getFont("default"));
	label->setHorizontalAlignment(Label::HA_LEFT);
	label->setVerticalAlignment(Label::VA_CENTER);
	label->setDrawBackground(true);
	setLabelPadding(label);
	label->setSkin(APPCONTROLLER->getSkin("title"));
	//label->update();
	layout->add(label);

	label = new Label(0,0, APPCONTROLLER->getScreenWidth()/2, height, NULL, right, 0, APPCONTROLLER->getFont("default"));
	label->setHorizontalAlignment(Label::HA_RIGHT);
	label->setVerticalAlignment(Label::VA_CENTER);
	label->setDrawBackground(true);
	setLabelPadding(label);
	label->setSkin(APPCONTROLLER->getSkin("title"));
	//label->update();
	layout->add(label);

	return layout;
}

Widget* create3SoftKeyBar(int height, const char *left, const char* middle, const char *right) {
	Layout *layout = new Layout(0, 0, APPCONTROLLER->getScreenWidth(), height, NULL, 3, 1);
	Label *label;

	label = new Label(0,0, APPCONTROLLER->getScreenWidth()/3, height, NULL, left, 0, APPCONTROLLER->getFont("default"));
	label->setHorizontalAlignment(Label::HA_LEFT);
	label->setVerticalAlignment(Label::VA_CENTER);
	label->setDrawBackground(true);
	setLabelPadding(label);
	label->setSkin(APPCONTROLLER->getSkin("title"));
	//label->update();
	layout->add(label);

	label = new Label(0,0, APPCONTROLLER->getScreenWidth()/3, height, NULL, middle, 0, APPCONTROLLER->getFont("default"));
	label->setHorizontalAlignment(Label::HA_CENTER);
	label->setVerticalAlignment(Label::VA_CENTER);
	label->setDrawBackground(true);
	setLabelPadding(label);
	label->setSkin(APPCONTROLLER->getSkin("title"));
	//label->update();
	layout->add(label);

	label = new Label(0,0, APPCONTROLLER->getScreenWidth()/3, height, NULL, right, 0, APPCONTROLLER->getFont("default"));
	label->setHorizontalAlignment(Label::HA_RIGHT);
	label->setVerticalAlignment(Label::VA_CENTER);
	label->setDrawBackground(true);
	setLabelPadding(label);
	label->setSkin(APPCONTROLLER->getSkin("title"));
	//label->update();
	layout->add(label);

	return layout;
}

// first child is listbox
Layout* createMainLayout(const char *title, const char *left, const char *right) {
	Layout* mainLayout = new Layout(0, 0, APPCONTROLLER->getScreenWidth(), APPCONTROLLER->getScreenHeight(), NULL, 1, 3);

	//Create Main Title
	Label* mainTitle = new Label(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTBAR, mainLayout);
	mainTitle->setCaption(title);
	mainTitle->setHorizontalAlignment(Label::HA_CENTER);
	mainTitle->setVerticalAlignment(Label::VA_CENTER);
	mainTitle->setFont(APPCONTROLLER->getFont("default"));
	mainTitle->setDrawBackground(true);
	mainTitle->setPaddingLeft(0);
	mainTitle->setPaddingRight(0);
	mainTitle->setSkin(APPCONTROLLER->getSkin("title"));
	mainTitle->update(); //Calculate the correct size

	//Create SoftKey Bar
	Widget *softKeys = createSoftKeyBar(HEIGHTBAR, left, right);

	//Create Listbox
	ListBox* listBox = new ListBox(0 , 0, APPCONTROLLER->getScreenWidth(), APPCONTROLLER->getScreenHeight()-softKeys->getHeight() - mainTitle->getHeight(), mainLayout, ListBox::LBO_VERTICAL, ListBox::LBA_LINEAR, true);
	listBox->setSkin(APPCONTROLLER->getSkin("default"));
	listBox->setPaddingLeft(HORIZONTALPADDING);
	listBox->setPaddingRight(HORIZONTALPADDING);
	listBox->setPaddingTop(VERTICALPADDING);
	listBox->setPaddingBottom(VERTICALPADDING);

	mainLayout->add(softKeys);

	//mainLayout->setSkin(NULL);
	/*mainLayout->setDrawBackground(true);
	mainLayout->setBackgroundColor(0);*/

	return mainLayout;
}

// first child is listbox
Layout* createMainLayout(const char *title, const char *left, const char* middle, const char *right) {
	Layout* mainLayout = new Layout(0, 0, APPCONTROLLER->getScreenWidth(), APPCONTROLLER->getScreenHeight(), NULL, 1, 3);

	//Create Main Title
	Label* mainTitle = new Label(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTBAR, mainLayout);
	mainTitle->setCaption(title);
	mainTitle->setHorizontalAlignment(Label::HA_CENTER);
	mainTitle->setVerticalAlignment(Label::VA_CENTER);
	mainTitle->setFont(APPCONTROLLER->getFont("default"));
	mainTitle->setDrawBackground(true);
	mainTitle->setPaddingLeft(0);
	mainTitle->setPaddingRight(0);
	mainTitle->setSkin(APPCONTROLLER->getSkin("title"));
	mainTitle->update(); //Calculate the correct size

	//Create SoftKey Bar
	Widget *softKeys = create3SoftKeyBar(HEIGHTBAR, left, middle, right);

	//Create Listbox
	ListBox* listBox = new ListBox(0 , 0, APPCONTROLLER->getScreenWidth(), APPCONTROLLER->getScreenHeight()-softKeys->getHeight() - mainTitle->getHeight(), mainLayout, ListBox::LBO_VERTICAL, ListBox::LBA_LINEAR, true);
	listBox->setSkin(APPCONTROLLER->getSkin("default"));
	listBox->setPaddingLeft(HORIZONTALPADDING);
	listBox->setPaddingRight(HORIZONTALPADDING);
	listBox->setPaddingTop(VERTICALPADDING);
	listBox->setPaddingBottom(VERTICALPADDING);

	mainLayout->add(softKeys);

	//mainLayout->setSkin(NULL);
	/*mainLayout->setDrawBackground(true);
	mainLayout->setBackgroundColor(0);*/

	return mainLayout;
}

